#!/usr/bin/env python

import sys
from dl_manager_class import DLManager

def get_extra_modules ( liwe, extra_id = 0, module = '', _X_LINES = 10, _X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:
		- extra_id:			SAFE_NUMBER
		- module:			SAFE_DB_STRING
		- _X_LINES:            		SAFE_NUMBER
                - _X_START_POINT:              	SAFE_NUMBER
                - _X_PAGE:              	SAFE_NUMBER
	"""
	dm = DLManager ( liwe )
	
   	if _X_START_POINT:
                start_point = _X_START_POINT
        else:
                start_point = _X_PAGE * _X_LINES

        limit = _X_LINES

	vals = dm.get_extra_modules ( extra_id = extra_id, module = module, start_point = start_point, limit = limit )

	if not vals:
		return { 'rows' : [], 'from_row' : 0, 'to_row' : 0, 'lines' : 0 }
	res = { 'rows' : vals [ 'rows' ] }

        count = 0
        for v in vals [ 'results' ]:
                v [ "_pos" ] = start_point + count
                res [ "row%d" % count ] = v
                count += 1

        res [ 'from_row' ] = start_point
        res [ 'to_row' ] = start_point + count
        res [ 'lines' ] = count

        return res

def add_extra_modules ( liwe, extra_id = 0, module = '', method = '', invoke = '', id_obj = 0 ):
	"""
	:INPUT:
		- extra_id:			SAFE_NUMBER
		- module:			SAFE_STRING
		- method:			SAFE_STRING
		- invoke:			SAFE_STRING
		- id_obj:			SAFE_NUMBER
	"""
	dm = DLManager ( liwe )
	last_id = dm.add_extra_modules ( extra_id = extra_id, module = module, 
				method = method, invoke = invoke, id_obj = id_obj )
	return { 'last_id' : last_id }

def add_file ( liwe, name = "", descr = "", files_tags = "", id_file = 0, id_obj = 0, module = "", kwargs = 0 ):
	"""
	:INPUT:
		- name:					SAFE_STRING
		- module:				SAFE_STRING
		- descr:				SAFE_STRING
		- files_tags:				SAFE_STRING
		- id_file : 				SAFE_NUMBER
		- id_obj :	 			SAFE_NUMBER

	:xPERMS:
		- create 
	"""

	sys.stderr.write ( "\n\n\n***** KEYS: %s\n\nkwargs = %s\n\n" % ( liwe.pyhp.uploads.keys (), kwargs ) )
	
	dm = DLManager ( liwe )

	if name: name = name.replace ( "'", "" )

	if not id_file:	
		keys = liwe.pyhp.uploads.keys ()
		if not keys:
			liwe.err_code = 101
			liwe.err_descr = "Manca il file %s %s" % ( name, descr )
			return {}

		for key in keys:
			my_file = liwe.pyhp.uploads [ key ]
			#sys.stderr.write ( "\n\n\n***** single KEY: %s\n\n" % key )
			#sys.stderr.write ( "\n\n---------------   DOWNLOAD MANAGER  admin.add_file: id_obj = %s, module = %s\n\n" % ( id_obj, module ) )
			dm.add_file ( file_obj = my_file , name = name , descr = descr, files_tags = files_tags, 
					id_file = id_file , id_obj = id_obj, module = module , kwargs = kwargs )

	if id_file:
		my_file = ''
		dm.add_file ( file_obj = my_file , name = name , descr = descr, files_tags = files_tags, 
				id_file = id_file , id_obj = id_obj, module = module , kwargs = kwargs )

	return { "res" : 'OK' }

def remove_attachments ( liwe, id_obj, module ):
	"""
	:INPUT:
		- id_obj :  				SAFE_NUMBER
		- module : module name 			SAFE_STRING

	:PERMS:
		- create
	"""
	dm = DLManager ( liwe )
	dm.remove_attachments ( id_obj = id_obj, module = module )

def remove_file ( liwe, path_name = "", id_file = 0, id_obj = 0, module = "" ):
	"""
	:INPUT:
		- path_name: path to file		SAFE_STRING
		- id_file :  file  id			SAFE_NUMBER
		- id_obj :  				SAFE_NUMBER
		- module : module name 			SAFE_STRING

	:PERMS:
		- create
	"""
	dm = DLManager ( liwe )
	res = dm.remove_file ( path_name = path_name, id_file = id_file , id_obj = id_obj, module = module )
	return { "res" : res }

def edit_file ( liwe, id_file = 0, id_obj = 0, module = "", action = "", kwargs = 0 ):
	"""
	:INPUT:
		- id_file : record id			SAFE_NUMBER
		- id_obj : 				SAFE_NUMBER
		- module : 				SAFE_STRING
		- action : action to invoke   		SAFE_STRING

	:PERMS:
		- edit 
	"""
	dm = DLManager ( liwe )
	res = dm.edit_file ( id_file = id_file, id_obj = id_obj, module = module, action = "", kwargs = kwargs )
	#sys.stderr.write ( "****************  EDIT FILE: %s\n" % res )
	return { "res" : res }
